package com.vogue.flowmeter.ui.fragment;


import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.vogue.flowmeter.R;
import com.vogue.flowmeter.api.event.MessageEvent;
import com.vogue.flowmeter.profile.data.VogueBleReadData;
import com.vogue.flowmeter.profile.data.VogueBleWriteData;
import com.vogue.flowmeter.ui.activity.ExternalSettingsActivity;
import com.vogue.flowmeter.ui.activity.MainActivity;
import com.vogue.flowmeter.ui.activity.UpdateHexActivity;
import com.vogue.flowmeter.util.DateUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 操作台
 */
public class DashboardFragment extends BaseFragment implements View.OnClickListener {

    //左侧菜单按钮
    @BindView(R.id.btn_cycle_time)
    Button btnCycleTime;
    @BindView(R.id.btn_devices_setting)
    Button btnDevicesSetting;
    @BindView(R.id.btn_peripherals)
    Button btnPeripherals;
    @BindView(R.id.btn_water_record)
    Button btnWaterRecord;
    @BindView(R.id.btn_equipment_control)
    Button btnEquipmentControl;


    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_is_ready)
    TextView isReady;


    //输入框背景
    @BindView(R.id.lay_et_sampling_period_background)
    LinearLayout layEtSamplingPeriodBackground;
    @BindView(R.id.lay_et_sending_period_background)
    LinearLayout layEtSendingPeriodBackground;
    @BindView(R.id.lay_et_calendar_clock_background)
    LinearLayout layEtCalendarClockBackground;
    @BindView(R.id.lay_et_devices_pid_background)
    LinearLayout layEtDevicesPidBackground;
    @BindView(R.id.lay_et_devices_card_num_background)
    LinearLayout layEtDevicesCardNumBackground;
    @BindView(R.id.lay_et_master_ip_background)
    LinearLayout layEtMasterIpBackground;
    @BindView(R.id.lay_et_devices_sub_ip_background)
    LinearLayout layEtDevicesSubIpBackground;
    @BindView(R.id.lay_et_peripherals_imei_background)
    LinearLayout layEtPeripheralsImeiBackground;
    @BindView(R.id.lay_et_peripherals_imsi_background)
    LinearLayout layEtPeripheralsImsiBackground;
    @BindView(R.id.lay_et_peripherals_usim_id_background)
    LinearLayout layEtPeripheralsUsimIdBackground;
    @BindView(R.id.lay_et_peripherals_csq_background)
    LinearLayout layEtPeripheralsCsqBackground;


    //输入框
    @BindView(R.id.et_sampling_period)
    EditText etSamplingPeriod;
    @BindView(R.id.et_sending_period)
    EditText etSendingPeriod;
    @BindView(R.id.et_calendar_clock)
    EditText etCalendarClock;
    @BindView(R.id.et_devices_pid)
    EditText etDevicesPid;
    @BindView(R.id.et_devices_card_num)
    EditText etDevicesCardNum;
    @BindView(R.id.et_master_ip)
    EditText etMasterIp;
    @BindView(R.id.et_devices_sub_ip)
    EditText etDevicesSubIp;
    @BindView(R.id.et_peripherals_imei)
    EditText etPeripheralsImei;
    @BindView(R.id.et_peripherals_imsi)
    EditText etPeripheralsImsi;
    @BindView(R.id.et_peripherals_usim_id)
    EditText etPeripheralsUsimId;
    @BindView(R.id.et_peripherals_csq)
    EditText etPeripheralsCsq;
    @BindView(R.id.tv_water_record_title)
    TextView tvWaterRecordTitle;
    @BindView(R.id.tv_water_record)
    TextView tvWaterRecord;


    //功能区读写按钮
    @BindView(R.id.btn_cycle_time_read_sampling_period)
    Button btnCycleTimeReadSamplingPeriod;
    @BindView(R.id.btn_cycle_time_write_sampling_period)
    Button btnCycleTimeWriteSamplingPeriod;
    @BindView(R.id.btn_cycle_time_read_sending_period)
    Button btnCycleTimeReadSendingPeriod;
    @BindView(R.id.btn_cycle_time_write_sending_period)
    Button btnCycleTimeWriteSendingPeriod;
    @BindView(R.id.btn_cycle_time_read_calendar_clock)
    Button btnCycleTimeReadCalendarClock;
    @BindView(R.id.btn_cycle_time_write_calendar_clock)
    Button btnCycleTimeWriteCalendarClock;
    @BindView(R.id.btn_devices_setting_read_devices_pid)
    Button btnDevicesSettingReadDevicesPid;
    @BindView(R.id.btn_devices_setting_write_devices_pid)
    Button btnDevicesSettingWriteDevicesPid;
    @BindView(R.id.btn_devices_setting_read_devices_card_num)
    Button btnDevicesSettingReadDevicesCardNum;
    @BindView(R.id.btn_devices_setting_write_devices_card_num)
    Button btnDevicesSettingWriteDevicesCardNum;
    @BindView(R.id.btn_devices_setting_read_master_ip)
    Button btnDevicesSettingReadMasterIp;
    @BindView(R.id.btn_devices_setting_write_master_ip)
    Button btnDevicesSettingWriteMasterIp;
    @BindView(R.id.btn_devices_setting_read_devices_sub_ip)
    Button btnDevicesSettingReadDevicesSubIp;
    @BindView(R.id.btn_devices_setting_write_devices_sub_ip)
    Button btnDevicesSettingWriteDevicesSubIp;

    @BindView(R.id.btn_peripherals_read)
    Button btnPeripheralsRead;
    @BindView(R.id.btn_history_read)
    Button btnHistoryRead;
    @BindView(R.id.btn_frozen_read)
    Button btnFrozenRead;
    @BindView(R.id.btn_external_read)
    Button btnExternalRead;
    @BindView(R.id.btn_master_equipment_upgrade)
    Button btnMasterEquipmentUpgrade;
    @BindView(R.id.btn_slave_equipment_upgrade)
    Button btnSlaveEquipmentUpgrade;
    @BindView(R.id.btn_system_restart)
    Button btnSystemRestart;
    @BindView(R.id.btn_remote_transmission)
    Button btnRemoteTransmission;
    @BindView(R.id.btn_external_parameters)
    Button btnExternalParameters;
    @BindView(R.id.btn_version)
    Button btnVersion;
    @BindView(R.id.btn_disconnect)
    Button btnDisconnect;

    @BindView(R.id.btn_200_test)
    Button btn200Test;




    //右侧功能区布局
    private View functionCycleTimeControl;
    private View functionDevicesSetting;
    private View functionEquipmentControl;
    private View functionPeripheralsControl;
    private View functionWaterRecordControl;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        ButterKnife.bind(this, root);
        //实例化功能页布局
        functionCycleTimeControl = root.findViewById(R.id.function_cycle_time);
        functionDevicesSetting = root.findViewById(R.id.function_devices_setting);
        functionPeripheralsControl = root.findViewById(R.id.function_peripherals);
        functionEquipmentControl = root.findViewById(R.id.function_equipment_control);
        functionWaterRecordControl = root.findViewById(R.id.function_water_record);

        //初始化进入
        btnCycleTime.setSelected(true);
        functionCycleTimeControl.setVisibility(View.VISIBLE);
        etCalendarClock.setKeyListener(null);
        //检查设备连接
        checkConnection();

        return root;
    }


    @Override
    public void onResume() {
        super.onResume();
        //回到页面进行判断蓝牙是否连接正常
        checkConnection();
        //clock = null;
        btnCycleTime.setSelected(true);
        functionCycleTimeControl.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 订阅消息  获取到消息类型  修改UI
     *
     * @param event
     */
    @Subscribe
    public void onMessageEvent(MessageEvent event) {

        Log.e(DashboardFragment.class.getName(), event.toString());

        switch (event.getOperator()) {
            // TODO 读取
            case "08":
                //读取软件版本号
                if ("rx".equals(event.getDirection())) {
                    tvVersion.setText("没有获取到版本");
                } else if ("tx".equals(event.getDirection())) {
                    tvVersion.setText(event.getMessage());
                }

                break;
            case "10":
                //读设备PID
                if ("rx".equals(event.getDirection())) {
                    etDevicesPid.setHint(event.getMessage());
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etDevicesPid.setText(event.getMessage());
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etDevicesPid.setHint(event.getMessage());
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "14":
                //读卡号
                if ("rx".equals(event.getDirection())) {
                    etDevicesCardNum.setHint(event.getMessage());
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etDevicesCardNum.setText(event.getMessage());
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etDevicesCardNum.setHint(event.getMessage());
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "15":
                //读设备masterIP
                if ("rx".equals(event.getDirection())) {
                    etMasterIp.setHint(event.getMessage());
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etMasterIp.setText(event.getMessage());
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etMasterIp.setHint(event.getMessage());
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "16":
                //读设备subIP
                if ("rx".equals(event.getDirection())) {
                    etDevicesSubIp.setHint(event.getMessage());
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etDevicesSubIp.setText(event.getMessage());
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etDevicesSubIp.setHint(event.getMessage());
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "19":
                //读历史水量记录
                if ("rx".equals(event.getDirection())) {
                    tvWaterRecord.setText("没有获取到历史水量记录");
                } else if ("tx".equals(event.getDirection())) {
                    tvWaterRecord.setText(event.getMessage());
                }

                break;
            case "1a":
                //读冻结水量记录
                if ("rx".equals(event.getDirection())) {
                    tvWaterRecord.setText("没有获取到冻结水量记录");
                } else if ("tx".equals(event.getDirection())) {
                    tvWaterRecord.setText(event.getMessage());
                }

                break;
            case "51":
                //读外接水表水量记录
                if ("rx".equals(event.getDirection())) {
                    tvWaterRecord.setText("没有获取到外接水表水量记录");
                } else if ("tx".equals(event.getDirection())) {
                    tvWaterRecord.setText(event.getMessage());
                }

                break;

            case "20":
                //读取采样周期
                if ("rx".equals(event.getDirection())) {
                    etSamplingPeriod.setHint(event.getMessage());
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etSamplingPeriod.setText(event.getMessage());
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etSamplingPeriod.setHint(event.getMessage());
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "21":
                //读取发送周期
                if ("rx".equals(event.getDirection())) {
                    etSendingPeriod.setHint(event.getMessage());
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etSendingPeriod.setText(event.getMessage());
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etSendingPeriod.setHint(event.getMessage());
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "22":
                //读取日历时钟
                if ("rx".equals(event.getDirection())) {
                    etCalendarClock.setHint(event.getMessage());
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etCalendarClock.setText(DateUtils.format(DateUtils.parse(event.getMessage()),DateUtils.DEFAULT_DATE_FORMAT));
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etCalendarClock.setHint(event.getMessage());
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }
                break;

            case "40":
                //读IMEI
                if ("rx".equals(event.getDirection())) {
                    etPeripheralsImei.setHint(event.getMessage());
                    layEtPeripheralsImeiBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etPeripheralsImei.setText(event.getMessage());
                    layEtPeripheralsImeiBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etPeripheralsImei.setHint(event.getMessage());
                    layEtPeripheralsImeiBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "41":
                //s读IMSI
                if ("rx".equals(event.getDirection())) {
                    etPeripheralsImsi.setHint(event.getMessage());
                    layEtPeripheralsImsiBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etPeripheralsImsi.setText(event.getMessage());
                    layEtPeripheralsImsiBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etPeripheralsImsi.setHint(event.getMessage());
                    layEtPeripheralsImsiBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "42":
                //读USIM ID
                if ("rx".equals(event.getDirection())) {
                    etPeripheralsUsimId.setHint(event.getMessage());
                    layEtPeripheralsUsimIdBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etPeripheralsUsimId.setText(event.getMessage());
                    layEtPeripheralsUsimIdBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etPeripheralsUsimId.setHint(event.getMessage());
                    layEtPeripheralsUsimIdBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "43":
                //读信号强度CSQ
                if ("rx".equals(event.getDirection())) {
                    etPeripheralsCsq.setHint(event.getMessage());
                    layEtPeripheralsCsqBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etPeripheralsCsq.setText(event.getMessage());
                    layEtPeripheralsCsqBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    etPeripheralsCsq.setHint(event.getMessage());
                    layEtPeripheralsCsqBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;

            // TODO 写入

            case "0a":
                showToast("系统重启");
                break;
            case "90":
                //写设备PID
                if ("rx".equals(event.getDirection())) {
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "94":
                //设置卡编号
                if ("rx".equals(event.getDirection())) {
                    etDevicesCardNum.setHint(event.getMessage());
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etDevicesCardNum.setText(event.getMessage());
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "95":
                //写设备masterIP
                if ("rx".equals(event.getDirection())) {
                    etMasterIp.setHint(event.getMessage());
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etMasterIp.setText(event.getMessage());
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "96":
                //写设备subIP
                if ("rx".equals(event.getDirection())) {
                    etDevicesSubIp.setHint(event.getMessage());
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etDevicesSubIp.setText(event.getMessage());
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "a0":
                //写采样周期
                if ("rx".equals(event.getDirection())) {
                    etSamplingPeriod.setHint(event.getMessage());
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etSamplingPeriod.setText(event.getMessage());
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "a1":
                //写发送周期
                if ("rx".equals(event.getDirection())) {
                    etSendingPeriod.setHint(event.getMessage());
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etSendingPeriod.setText(event.getMessage());
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }
                break;
            case "a2":
                //写日历时钟
                if ("rx".equals(event.getDirection())) {
                    etCalendarClock.setHint(event.getMessage());
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_blue);
                } else if ("tx".equals(event.getDirection())) {
                    etCalendarClock.setText(DateUtils.format(DateUtils.parse(event.getMessage()),DateUtils.DEFAULT_DATE_FORMAT));
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_accent);
                } else {
                    layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
                }

                break;
            case "c0":
                if ("rx".equals(event.getDirection())) {
                    showToast("远端强制发送==已发送: " + event.getMessage());
                } else if ("tx".equals(event.getDirection())) {
                    showToast("远端强制常规发送==已收到: " + event.getMessage());
                } else {
                    showToast("远端强制常规发送");
                }

                break;
            case "f4":
                Log.e(DashboardFragment.class.getName(), "" + event.toString());
                showToast("测试200字节: " + event.getMessage());
                break;
            case "connection":
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        checkConnection();
                    }
                });
                break;

            default:
                showToast("解析失败");
        }
    }


    private void checkConnection() {
        if (getLocalBinder() != null) {
            if (getLocalBinder().isReady()) {
                BluetoothDevice device = getLocalBinder().getBluetoothDevice();
                isReady.setText("已准备就绪(" + device.getName() + "--" + device.getAddress() + ")");
            } else {
                showToast("设备未就绪,请先连接相关设备");
                ((MainActivity) requireActivity()).changeNavigation(R.id.navigation_home);
                isReady.setText("设备未就绪");
                return;
            }
        } else {
            showToast("设备未就绪,请先连接相关设备");
            ((MainActivity) requireActivity()).changeNavigation(R.id.navigation_home);
            isReady.setText("设备未就绪");
            return;
        }
    }


    @OnClick({R.id.btn_cycle_time_read_sampling_period, R.id.btn_cycle_time_write_sampling_period,
            R.id.btn_cycle_time_read_sending_period, R.id.btn_cycle_time_write_sending_period,
            R.id.btn_cycle_time_read_calendar_clock, R.id.btn_cycle_time_write_calendar_clock,
            R.id.btn_devices_setting_read_devices_pid, R.id.btn_devices_setting_write_devices_pid,
            R.id.btn_devices_setting_read_devices_card_num, R.id.btn_devices_setting_write_devices_card_num,
            R.id.btn_devices_setting_read_master_ip, R.id.btn_devices_setting_write_master_ip,
            R.id.btn_devices_setting_read_devices_sub_ip, R.id.btn_devices_setting_write_devices_sub_ip,
            R.id.et_calendar_clock, R.id.btn_external_read, R.id.btn_cycle_time, R.id.btn_devices_setting,
            R.id.btn_peripherals, R.id.btn_water_record, R.id.btn_equipment_control,
            R.id.btn_peripherals_read, R.id.btn_history_read, R.id.btn_frozen_read, R.id.btn_master_equipment_upgrade,
            R.id.btn_slave_equipment_upgrade, R.id.btn_system_restart, R.id.btn_remote_transmission,
            R.id.btn_external_parameters, R.id.btn_version, R.id.btn_disconnect})
    public void onClick(View view) {
        //左侧按钮选择状态
        btnCycleTime.setSelected(false);
        btnDevicesSetting.setSelected(false);
        btnPeripherals.setSelected(false);
        btnWaterRecord.setSelected(false);
        btnEquipmentControl.setSelected(false);

        //右侧功能区隐藏
        functionCycleTimeControl.setVisibility(View.GONE);
        functionDevicesSetting.setVisibility(View.GONE);
        functionPeripheralsControl.setVisibility(View.GONE);
        functionWaterRecordControl.setVisibility(View.GONE);
        functionEquipmentControl.setVisibility(View.GONE);

        //输入框的初始背景
        layEtSamplingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtSendingPeriodBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtCalendarClockBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);

        layEtDevicesPidBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtDevicesCardNumBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtMasterIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtDevicesSubIpBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);

        layEtPeripheralsImeiBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtPeripheralsImsiBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtPeripheralsUsimIdBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);
        layEtPeripheralsCsqBackground.setBackgroundResource(R.drawable.ic_background_et_nomal);

        //检查蓝牙连接
        checkConnection();

        switch (view.getId()) {
            //左侧按钮点击事件
            case R.id.btn_cycle_time:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_devices_setting:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_peripherals:
                btnPeripherals.setSelected(true);
                functionPeripheralsControl.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_water_record:
                btnWaterRecord.setSelected(true);
                functionWaterRecordControl.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_equipment_control:
                btnEquipmentControl.setSelected(true);
                functionEquipmentControl.setVisibility(View.VISIBLE);
                break;

            //右侧功能区侧按钮点击事件
            case R.id.btn_cycle_time_read_sampling_period:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                if (getLocalBinder() != null) {
                    //执行读取采集周期
                    getLocalBinder().read(VogueBleReadData.ReadSamplingPeriod());
                }
                break;
            case R.id.btn_cycle_time_write_sampling_period:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                String samplingPeriod = etSamplingPeriod.getText().toString().trim();

                if (getLocalBinder() != null && !TextUtils.isEmpty(samplingPeriod)) {
                    //执行写入采集周期 2字节 单位分钟
                    getLocalBinder().write(VogueBleWriteData.WriteSamplingPeriod(samplingPeriod));
                }
                break;
            case R.id.btn_cycle_time_read_sending_period:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                if (getLocalBinder() != null) {
                    //执行读取发送周期
                    getLocalBinder().read(VogueBleReadData.ReadSendingPeriod());
                }
                break;
            case R.id.btn_cycle_time_write_sending_period:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                String sendingPeriod = etSendingPeriod.getText().toString().trim();
                if (getLocalBinder() != null&&!TextUtils.isEmpty(sendingPeriod)) {
                    //执行写入发送周期 2字节 单位分钟
                    getLocalBinder().write(VogueBleWriteData.WriteSendingPeriod(sendingPeriod));
                }
                break;
            case R.id.btn_cycle_time_read_calendar_clock:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
                if (getLocalBinder() != null) {
                    //执行读取 日历时钟
                    getLocalBinder().read(VogueBleReadData.ReadCalendarClock());
                }
                break;
            case R.id.btn_cycle_time_write_calendar_clock:
                btnCycleTime.setSelected(true);
                functionCycleTimeControl.setVisibility(View.VISIBLE);
               // String calendarClock = etCalendarClock.getText().toString();

                if (getLocalBinder() != null) {

                    //执行写入 日历时钟 6字节 yyMMddhhmmss(20-06-01 06:00:22) (BCD码:4个2进制表现一个十进制)
                    String d = DateUtils.format(new Date().getTime(), DateUtils.BCD_DATE_FORMAT);
                    getLocalBinder().write(VogueBleWriteData.WriteCalendarClock(d));
                }

                break;
            case R.id.btn_devices_setting_read_devices_pid:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                if (getLocalBinder() != null) {
                    //执行读取 设备PID
                    getLocalBinder().read(VogueBleReadData.ReadEquipmentPid());
                }

                break;
            case R.id.btn_devices_setting_write_devices_pid:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                String devicesPid = etDevicesPid.getText().toString().trim();
                if (getLocalBinder() != null && !TextUtils.isEmpty(devicesPid)) {
                    //执行写入 设备PID  不定长
                    getLocalBinder().write(VogueBleWriteData.WriteEquipmentPid(devicesPid));
                }

                break;
            case R.id.btn_devices_setting_read_devices_card_num:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 设备CardNumber
                    getLocalBinder().read(VogueBleReadData.ReadSystemCardNumber());
                }
                break;
            case R.id.btn_devices_setting_write_devices_card_num:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                String devicesCardNum = etDevicesCardNum.getText().toString().trim();
                if (getLocalBinder() != null && !TextUtils.isEmpty(devicesCardNum) ) {
                    //执行写入 设备卡编号 不定长
                    getLocalBinder().write(VogueBleWriteData.WriteSystemCardNumber(devicesCardNum));
                }
                break;
            case R.id.btn_devices_setting_read_master_ip:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                if (getLocalBinder() != null) {
                    //执行读取 设备主IP
                    getLocalBinder().read(VogueBleReadData.ReadEquipmentMasterIp());
                }
                break;
            case R.id.btn_devices_setting_write_master_ip:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);
                String masterIp = etMasterIp.getText().toString().trim();
                if (getLocalBinder() != null && !TextUtils.isEmpty(masterIp)) {
                    //执行写入 设备主IP 不定长
                    getLocalBinder().write(VogueBleWriteData.WriteEquipmentMasterIp(masterIp.replace(":", ",")));
                }
                break;
            case R.id.btn_devices_setting_read_devices_sub_ip:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 设备从IP
                    getLocalBinder().read(VogueBleReadData.ReadEquipmentSubIp());
                }
                break;
            case R.id.btn_devices_setting_write_devices_sub_ip:
                btnDevicesSetting.setSelected(true);
                functionDevicesSetting.setVisibility(View.VISIBLE);

                String devicesSubIp = etDevicesSubIp.getText().toString().trim();

                if (getLocalBinder() != null && !TextUtils.isEmpty(devicesSubIp)) {
                    //执行写入 设备从IP 不定长
                    getLocalBinder().write(VogueBleWriteData.WriteEquipmentSubIp(devicesSubIp.replace(":", ",")));
                }
                break;


            case R.id.btn_peripherals_read:
                btnPeripherals.setSelected(true);
                functionPeripheralsControl.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 IMEI
                    getLocalBinder().read(VogueBleReadData.ReadImei());
                    //执行读取 IMSI
                    getLocalBinder().read(VogueBleReadData.ReadImsi());
                    //执行读取 USIMID
                    getLocalBinder().read(VogueBleReadData.ReadUsimId());
                    //执行读取 信号强度CSQ
                    getLocalBinder().read(VogueBleReadData.ReadCsq());
                }
                break;

            case R.id.btn_history_read:
                tvWaterRecordTitle.setText("历史水量");
                btnWaterRecord.setSelected(true);
                functionWaterRecordControl.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 历史水量 返回37*n  最多四条
                    getLocalBinder().read(VogueBleReadData.ReadHistoryWaterQuantity());
                }
                break;

            case R.id.btn_frozen_read:
                tvWaterRecordTitle.setText("冻结水量");
                btnWaterRecord.setSelected(true);
                functionWaterRecordControl.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 冻结水量 返回37*n  最多四条
                    getLocalBinder().read(VogueBleReadData.ReadFrozenWaterQuantity());
                }
                break;

            case R.id.btn_external_read:
                tvWaterRecordTitle.setText("外接水表水量");
                btnWaterRecord.setSelected(true);
                functionWaterRecordControl.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行读取 外接水表水量 返回37
                    getLocalBinder().read(VogueBleWriteData.ReadMeterWaterQuantity("ext"));
                }
                break;
            case R.id.btn_master_equipment_upgrade:
                btnEquipmentControl.setSelected(false);
                functionEquipmentControl.setVisibility(View.GONE);
                startActivity(new Intent(requireActivity(), UpdateHexActivity.class).putExtra("upgrade", "master"));
                break;
            case R.id.btn_slave_equipment_upgrade:
                btnEquipmentControl.setSelected(false);
                functionEquipmentControl.setVisibility(View.GONE);
                startActivity(new Intent(requireActivity(), UpdateHexActivity.class).putExtra("upgrade", "slave"));
                break;
            case R.id.btn_system_restart:
                btnEquipmentControl.setSelected(true);
                functionEquipmentControl.setVisibility(View.VISIBLE);
                //执行系统重启 3字节
                if (getLocalBinder() != null) {
                    getLocalBinder().read(VogueBleWriteData.SystemRestart("rst"));
                }
                break;
            case R.id.btn_remote_transmission:
                btnEquipmentControl.setSelected(true);
                functionEquipmentControl.setVisibility(View.VISIBLE);

                if (getLocalBinder() != null) {
                    //执行远端发送 4字节
                    getLocalBinder().read(VogueBleWriteData.RemoteMandatoryNormalSend("send"));
                }
                break;
            case R.id.btn_external_parameters:
                btnEquipmentControl.setSelected(false);
                functionEquipmentControl.setVisibility(View.GONE);
                //跳转到外部参数设置
                startActivity(new Intent(requireActivity(), ExternalSettingsActivity.class));
                break;
            case R.id.btn_version:
                btnEquipmentControl.setSelected(true);
                functionEquipmentControl.setVisibility(View.VISIBLE);
                //执行获取版本
                if (getLocalBinder() != null) {
                    getLocalBinder().read(VogueBleReadData.ReadVersion());
                }
                break;
            case R.id.btn_disconnect:
                btnEquipmentControl.setSelected(false);
                functionEquipmentControl.setVisibility(View.GONE);
                if (getLocalBinder() != null) {
                    showToast("断开连接");
                    getLocalBinder().disconnect();
                }
                break;
        }
    }

    @OnClick(R.id.btn_200_test)
    public void onClick() {
        String test = "abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345";
        getLocalBinder().sent(VogueBleWriteData.Write200TEST(test.substring(0, 110)));
    }


}